<?php defined('SYSPATH') or die('No direct script access.');

/**
 * 租车应用
 */
class Controller_Pc_Api_V2_Car extends Controller_Pc_Api_V2_Base
{

    // 数据请求结果
    private $result;

    /**
     * 参数验证对象
     * @var Request_Api_V2_Car
     */
    private $validation;

    /**
     * 租车服务
     * @var Service_Api_Car
     */
    private $service = null;


    public function before()
    {
        parent::before();
        $this->result = array(
            'status' => true,
            'msg' => '操作成功',
            'data' => array()
        );
        $this->validation = Request_Api_V2_Factory::create('car', $this->request_params);
        $this->service = new Service_Api_Car();
    }
    public function after()
    {
        $this->send_datagrams($this->client_info['id'], $this->result, $this->client_info['secret_key']);
    }

    // 获取推荐租车
    public function action_recommend()
    {
        try {
            $this->validation->recommend()->check();
            $data = $this->service->recommend($this->mid);
            $this->result['data'] = $data;
        } catch (Exception $e) {
            $this->result = array('status' => false, 'msg' => $e->getMessage());
        }
    }

    // 获取租车车型
    public function action_car_kind_list()
    {
        try {
            $data = $this->service->car_kind_list();
            $this->result['data'] = $data;
        } catch (Exception $e) {
            $this->result = array('status' => false, 'msg' => $e->getMessage());
        }
    }

    /**
     * 根据车型获取列表
     */
    public function action_get_attr_list()
    {
        try {
            $this->validation->get_attr_list()->check();
            $kind_id = Arr::get($this->request_params, 'kind_id', '');
            $page    = Arr::get($this->request_params, 'page', 1);
            $row     = Arr::get($this->request_params, 'pagesize', 10);
            $offset  = $page ? ($page - 1) * $row : 0;
            $list  = $this->service->get_cars_by_kind($kind_id, $offset, $row, $this->mid);
            $this->result['data'] = $list;
        } catch (Exception $e) {
            $this->result['status'] = false;
            $this->result['msg']    = $e->getMessage();
        }
    }

    // 筛选条件
    public function action_query_condition()
    {
        try {
            $list  = $this->service->query_condition();
            $this->result['data'] = $list;
        } catch (Exception $e) {
            $this->result['status'] = false;
            $this->result['msg']    = $e->getMessage();
        }
    }

    // 条件筛选列表
    public function action_general_query()
    {
        try {
            $this->validation->general_query()->check();
            $params = array(
                'pagesize' => Arr::get($this->request_params, 'pagesize', 5),//每页数量
                'page' => Arr::get($this->request_params, 'page', 1),//每页数量
                'keyword' => Arr::get($this->request_params, 'keyword', ''),//关键词
                'start_time' => strtotime($this->request_params['start_time']),//开始时间
                'end_time' => strtotime($this->request_params['end_time']),//结束时间
                'car_kind_id' => Arr::get($this->request_params, 'car_kind_id', ''),//租车类型id
                'mid' => Arr::get($this->request_params, 'mid', ''),//租车类型id
                'use_type' => Arr::get($this->request_params, 'use_type', 0),//0：包车 1：租车
                'time_desc'=>Arr::get($this->request_params, 'time_desc', ''),//包车租车日期
            );
            //参数
            $path   = $this->request_params['path'];
            list($params['dest_id'], $params['price_id'], $params['sorttype'], $params['attrid'], $params['page']) = explode('-', $path);
            $this->result['data'] = $this->service->search($params);
        } catch (Exception $e) {
            $this->result['status'] = false;
            $this->result['msg']    = $e->getMessage();
        }
        $this->send_datagrams($this->client_info['id'], $this->result, $this->client_info['secret_key']);
    }

    // 根据日期获得该套餐列表
    public function action_get_day_suitlist()
    {
        try {
            $this->validation->get_day_suitlist()->check();
            $this->result['data']        = $this->service->get_suit_list_by_day($this->request_params);
        } catch (Exception $e) {
            $this->result['status'] = false;
            $this->result['msg']    = $e->getMessage();
        }
    }

    // 租车详情
    public function action_detail()
    {
        try {
            $this->validation->detail()->check();
            $data = $this->service->info($this->request_params);
            if (empty($data)) {
                throw new Exception('查询车辆信息失败');
            }
            $this->result['data'] = $data;
        } catch (Exception $e) {
            $this->result['status'] = false;
            $this->result['msg']    = $e->getMessage();
        }
        $this->send_datagrams($this->client_info['id'], $this->result, $this->client_info['secret_key']);
    }




    // 预订页的每日报价
    public function action_get_everyday_price()
    {
        try {
            $this->validation->get_everyday_price()->check();
            $this->result['data'] = $this->service->get_everyday_price($this->request_params);
        } catch (Exception $e) {
            $this->result['status'] = false;
            $this->result['msg']    = $e->getMessage();
        }
        $this->send_datagrams($this->client_info['id'], $this->result, $this->client_info['secret_key']);
    }

    /**
     * @function 获取租车套餐信息
     */
    public function action_car_suit()
    {
        try {
            $this->validation->car_suit()->check();
            $this->result['data']    = $this->service->car_suit($this->request_params);
        } catch (Exception $e) {
            $this->result['status'] = false;
            $this->result['msg']    = $e->getMessage();
        }
        $this->send_datagrams($this->client_info['id'], $this->result, $this->client_info['secret_key']);
    }


    // 创建订单
    public function action_add_order()
    {
        try {

            $this->validation->add_order()->check();

            $this->request_params['member_info'] = $this->member_info;
            $this->result['data'] = $this->service->create_order($this->request_params);
        } catch (Exception $e) {
            $this->result['msg']    = $e->getMessage();
            $this->result['status'] = false;
        }

        $this->send_datagrams($this->client_info['id'], $this->result, $this->client_info['secret_key']);
    }

    //获取自提点
    public function action_get_self_pickup_address(){
        try {
            $this->result['data'] = $this->service->get_self_pickup_address($this->request_params['product_id']);
        } catch (Exception $e) {
            $this->result['msg']    = $e->getMessage();
            $this->result['status'] = false;
        }
        $this->send_datagrams($this->client_info['id'], $this->result, $this->client_info['secret_key']);
    }

}